package global

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
	"path/filepath"
)

var Logger *zap.SugaredLogger

func InitLogger() {
	encoder := getEncoder()
	coreRun := zapcore.NewCore(encoder, getLogWriter(Conf.Path.Log), zapcore.DebugLevel)
	Logger = zap.New(coreRun, zap.AddCaller()).Sugar()
	defer Logger.Sync()

}

func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	return zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
}
func getLogWriter(logfile string) zapcore.WriteSyncer {
	file, _ := os.Create(filepath.Join(Conf.Path.WorkDir, logfile))
	return zapcore.AddSync(file)
}
